CFLAGS=  -g -Wextra -Wall # -g, -O3 , ... par exemple
LDFLAGS=-ll -ly # les bibliotheques necessaires
CC=gcc

all: rubic

utils.o: utils.c utils.h
	$(CC) $(CFLAGS) -c $<

hash.o: hash.c hash.h
	$(CC) $(CFLAGS) -c $<

context.o: context.c context.h
	$(CC) $(CFLAGS) -c $<

lex.yy.c: scanner.l
	lex $<

y.tab.c: parse.y
	yacc -d $<

y.tab.o: y.tab.c y.tab.h utils.h hash.h
	$(CC) $(CFLAGS) -c $<

lex.yy.o: lex.yy.c
	$(CC) $(CFLAGS) -c $<

rubic: y.tab.o lex.yy.o utils.o hash.o context.o
	$(CC) -o $@ $^ $(LDFLAGS)

clean:
	rm *.o y.tab.h y.tab.c lex.yy.c
	rm ../test/*.ll
	rm ../build/*

test: 
	make
	./rubic while1 < ../test/while1.rb
	./rubic if1 < ../test/if1.rb
	./rubic if2 < ../test/if2.rb
	./rubic if3 < ../test/if3.rb
	./rubic add1 < ../test/add1.rb
	./rubic add2 < ../test/add2.rb
	./rubic add3 < ../test/add3.rb
	./rubic mul1 < ../test/mul1.rb
	./rubic mul2 < ../test/mul2.rb
	./rubic mul3 < ../test/mul3.rb
	./rubic fonction1 < ../test/fonction1.rb
	./rubic fonction2 < ../test/fonction2.rb
	./rubic error1 < ../test/error1.rb
	./rubic error2 < ../test/error2.rb
	./rubic error3 < ../test/error3.rb
	./rubic error4 < ../test/error4.rb
	./rubic error5 < ../test/error5.rb
	./rubic error6 < ../test/error6.rb

compil-mac.bc: compil-mac.bc
	llvm-as-mp-2.9 ../test/while1.ll -f -o ../build/while1.bc
	llvm-as-mp-2.9 ../test/if1.ll -f -o ../build/if1.bc
	llvm-as-mp-2.9 ../test/if2.ll -f -o ../build/if2.bc
	llvm-as-mp-2.9 ../test/if3.ll -f -o ../build/if3.bc
	llvm-as-mp-2.9 ../test/add1.ll -f -o ../build/add1.bc
	llvm-as-mp-2.9 ../test/add2.ll -f -o ../build/add2.bc
	llvm-as-mp-2.9 ../test/add3.ll -f -o ../build/add3.bc
	llvm-as-mp-2.9 ../test/mul1.ll -f -o ../build/mul1.bc
	llvm-as-mp-2.9 ../test/mul2.ll -f -o ../build/mul3.bc
	llvm-as-mp-2.9 ../test/mul3.ll -f -o ../build/mul3.bc
	llvm-as-mp-2.9 ../test/fonction1.ll -f -o ../build/fonction1.bc
	llvm-as-mp-2.9 ../test/fonction2.ll -f -o ../build/fonction2.bc

compil-mac: 
	llvm-ld-mp-2.9 -o=../build/while1 -native ../build/while1.bc
	llvm-ld-mp-2.9 -o=../build/if1 -native ../build/if1.bc
	llvm-ld-mp-2.9 -o=../build/if2 -native ../build/if2.bc
	llvm-ld-mp-2.9 -o=../build/if3 -native ../build/if3.bc
	llvm-ld-mp-2.9 -o=../build/add1 -native ../build/add1.bc
	llvm-ld-mp-2.9 -o=../build/add2 -native ../build/add2.bc
	llvm-ld-mp-2.9 -o=../build/add3 -native ../build/add3.bc
	llvm-ld-mp-2.9 -o=../build/mul1 -native ../build/mul1.bc
	llvm-ld-mp-2.9 -o=../build/mul2 -native ../build/mul2.bc
	llvm-ld-mp-2.9 -o=../build/mul3 -native ../build/mul3.bc
	llvm-ld-mp-2.9 -o=../build/fonction1 -native ../build/fonction1.bc
	llvm-ld-mp-2.9 -o=../build/fonction2 -native ../build/fonction2.bc
compil-linux.bc:  
	llvm-as ../test/while1.ll -f -o ../build/while1.bc
	llvm-as ../test/if1.ll -f -o ../build/if1.bc
	llvm-as ../test/if2.ll -f -o ../build/if2.bc
	llvm-as ../test/if3.ll -f -o ../build/if3.bc
	llvm-as ../test/add1.ll -f -o ../build/add1.bc
	llvm-as ../test/add2.ll -f -o ../build/add2.bc
	llvm-as ../test/add3.ll -f -o ../build/add3.bc
	llvm-as ../test/mul1.ll -f -o ../build/mul1.bc
	llvm-as ../test/mul2.ll -f -o ../build/mul2.bc
	llvm-as ../test/mul3.ll -f -o ../build/mul3.bc
	llvm-as ../test/fonction1.ll -f -o ../build/fonction1.bc
	llvm-as ../test/fonction2.ll -f -o ../build/fonction2.bc

compil-linux: compil-linux.bc
	llvm-ld -o=../build/while1 -native ../build/while1.bc
	llvm-ld -o=../build/if1 -native ../build/if1.bc
	llvm-ld -o=../build/if2 -native ../build/if2.bc
	llvm-ld -o=../build/if3 -native ../build/if3.bc
	llvm-ld -o=../build/add1 -native ../build/add1.bc
	llvm-ld -o=../build/add2 -native ../build/add2.bc
	llvm-ld -o=../build/add3 -native ../build/add3.bc
	llvm-ld -o=../build/mul1 -native ../build/mul1.bc
	llvm-ld -o=../build/mul2 -native ../build/mul2.bc
	llvm-ld -o=../build/mul3 -native ../build/mul3.bc
	llvm-ld -o=../build/fonction1 -native ../build/fonction1.bc
	llvm-ld -o=../build/fonction2 -native ../build/fonction2.bc